package com.chmingx.admin.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@Controller
public class FileController {
    @GetMapping("/file")
    public String filePage() {
        return "form/file";
    }

    /**
     * MultipartFile 自动封装了上传过来的文件
     * @return
     */
    @PostMapping("/file")
    public String upload(@RequestParam("username") String username,
                         @RequestParam("email") String email,
                         @RequestPart("avatar") MultipartFile avatar,
                         @RequestPart("photos") MultipartFile[] photos) throws IOException {
        System.out.println(username);
        System.out.println(email);
        if (!avatar.isEmpty()) {
            String originalFilename = avatar.getOriginalFilename();
            avatar.transferTo(new File("D:\\tmp\\" + originalFilename));
        }
        if (photos.length > 0) {
            for (MultipartFile photo : photos) {
                if (!photo.isEmpty()) {
                    photo.transferTo(new File("D:\\tmp\\" + photo.getOriginalFilename()));
                }
            }
        }
        return "index";
    }
}
